package com.extsci.webmail.common.util;

import org.apache.commons.lang.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.DecimalFormat;

/**
 * 文件工具类
 * @author ccoke
 */
public class MyFileUtils {
    public final static String FILE_SEPARATOR = "/";
    public final  static String UNDERLINE = "_";
    public static final String RESOURCE = "resource";
    public static final String TEMP = "temp";
    public static final String UPLOAD = "upload";


    /**
     * 将文件存储到绝对路径
     */
    public static void saveMultipartFile(MultipartFile file, String absolutePath, String name) throws IOException {
        file.transferTo(new File(contactUri(absolutePath, FILE_SEPARATOR, name)));
    }

    /**
     * 拼接字符串
     */
    public static String contactUri(String ... str){
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < str.length; i++) {
            sb.append(str[i]);
        }
        return sb.toString();
    }

    /**
     * 创建目录
     */
    public static void createDirs(String absolutePath){
        File f2 = new File(absolutePath);
        f2.mkdirs();
    }


    /**
     * 从URL获取文件路径
     */
    public static String getFilePathByURL(String url){
        // 获取第三个"/"
        int index = -1;
        for (int i = 0; i < 3; i++) {
            index = url.indexOf(FILE_SEPARATOR,index + 1);
        }
        if (index == -1){
            return "";
        }
        // 截取路径
        return index != -1 ? url.substring(index) : "";
    }

    /**
     * 判断文件是否存在
     */
    public static boolean isExists(String path){
        File file = new File(path);
        return file.exists();
    }

    /**
     * 删除文件
     */
    public static void delete(String filePath){
        File file = new File(filePath);
        if(file.exists()){
            file.delete();
        }
    }

    /**
     * 获取文件名(a.txt => a)
     */
    public static String getFileName(String fileName){
        int index = fileName.indexOf(UNDERLINE);
        return fileName.substring(index + 1);
    }

    /**
     * 从URI获取文件名称
     */
    public static String getFileNameByURI(String uri){
        String fileName = uri.substring(uri.lastIndexOf(FILE_SEPARATOR) + 1);
        return getFileName(fileName);
    }

    /**
     * 获取文件大小
     */
    public static String  getSize(String filePath){
        File file = new File(filePath);
        return parseFileSize(file.length());
    }

    /**
     * 文件大小转换
     */
    private static String parseFileSize(long sizeL){
        String size = "";
        DecimalFormat df = new DecimalFormat("#.00");
        if (sizeL < 1024) {
            size = df.format((double) sizeL) + "BT";
        } else if (sizeL < 1048576) {
            size = df.format((double) sizeL / 1024) + "KB";
        } else if (sizeL < 1073741824) {
            size = df.format((double) sizeL / 1048576) + "MB";
        } else {
            size = df.format((double) sizeL / 1073741824) +"GB";
        }
        return size;
    }

    /**
     * 特殊字符转义
     */
    public static String changeSpecialWord(String word) {
        return word
                .replace("+", "%2B")
                .replace(" ", "%20")
                .replace("/", "%2F")
                .replace("?", "%3F")
                .replace("#", "%23")
                .replace("&", "%26")
                .replace("=", "%3D");
    }

    /**
     * 递归删除目录
     */
    public static void deleteDir(File dir){
        if(dir.isDirectory()){
            File[] files = dir.listFiles();
            for(int i=0; i<files.length; i++) {
                deleteDir(files[i]);
            }
        }
        dir.delete();
    }

}
